package com.lyf.scm.admin.domain.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import com.lyf.scm.admin.api.dto.*;
import com.lyf.scm.admin.domain.remote.allocationservice.facade.SalesPlanDirectlyAccuracyFacade;
import com.lyf.scm.admin.domain.remote.item.facade.ItemCoreFacade;
import com.lyf.scm.admin.domain.service.SalesPlanDirectlyAccuracyService;
import com.lyf.scm.admin.domain.util.BaseSearchUtil;
import com.lyf.scm.common.enums.DataDisplayCycleEnum;
import com.lyf.scm.common.enums.ExcelExportTypeEnum;
import com.lyf.scm.common.enums.ResponseMsg;
import com.lyf.scm.common.model.ExcelExportDTO;
import com.lyf.scm.common.model.SalesPlanSelectDTO;
import com.lyf.scm.common.util.date.MyDateUtil;
import com.rome.arch.core.exception.RomeException;
import com.rome.user.common.utils.UserContext;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 直营预测计划_销售计划准确率
 *
 * @author wangchlei
 * @date 2020/08/13
 */
@Slf4j
@Service
public class SalesPlanDirectlyAccuracyImpl implements SalesPlanDirectlyAccuracyService {
    @Resource
    private SalesPlanDirectlyAccuracyFacade salesPlanDirectlyAccuracyFacade;

    @Resource
    private ItemCoreFacade itemCoreFacade;

    /**
     * 按条件查询直营预测计划_销售计划准确率(周)
     *
     * @param salesPlanDirectlyAccuracyDTO
     * @return {@link PlanResponseDTO<SalesPlanDirectlyAccuracyInfoDTO>}
     **/
    @Override
    public PlanResponseDTO<SalesPlanDirectlyAccuracyInfoDTO> listWeekBySelective(SalesPlanDirectlyAccuracyDTO salesPlanDirectlyAccuracyDTO) {
        //获得最低级品类查询条件
        List<SalesPlanSelectDTO> categorySalesPlanSelectDTOs = itemCoreFacade.getSubcategoryCode(salesPlanDirectlyAccuracyDTO.getCategoryCodeList());
        //赋值品类搜索条件
        BaseSearchUtil.setSearchFields(salesPlanDirectlyAccuracyDTO, null, categorySalesPlanSelectDTOs, null);
        //根据起始时间获取14周的日期数据
        List<Map<String, Date>> weekDates = MyDateUtil.getWeekDates(salesPlanDirectlyAccuracyDTO.getStartTime(), DataDisplayCycleEnum.WEEKLY_FOURTEEN.getValue());
        salesPlanDirectlyAccuracyDTO.setWeekDateList(weekDates);
        //设置结束日期，方便sql查询命中索引
        salesPlanDirectlyAccuracyDTO.setEndTime(MyDateUtil.getLastDate(null, weekDates));
        salesPlanDirectlyAccuracyDTO.setCreator(UserContext.getUserId());
        //数据展示周期
        PageInfo<SalesPlanDirectlyAccuracyInfoDTO> pageInfo = salesPlanDirectlyAccuracyFacade.listWeekBySelective(salesPlanDirectlyAccuracyDTO);
        PlanResponseDTO<SalesPlanDirectlyAccuracyInfoDTO> planResponse = new PlanResponseDTO();
        //格式化日期格式
        planResponse.setInfoList(pageInfo.getList());
        planResponse.setDateList(MyDateUtil.processCyclesDate(weekDates, "M.dd"));
        planResponse.setTotal(pageInfo.getTotal());
        return planResponse;
    }

    /**
     * 图表数据接口
     *
     * @param salesPlanDirectlyAccuracyDTO
     * @return {@link PlanResponseDTO<SalePlanDirectlyDifferinInfoDTO>}
     **/
    @Override
    public EChartsDTO eChartsData(SalesPlanDirectlyAccuracyDTO salesPlanDirectlyAccuracyDTO) {
        Date startTime = salesPlanDirectlyAccuracyDTO.getStartTime();
        Date endTime = salesPlanDirectlyAccuracyDTO.getEndTime();
        List<Map<String, Date>> weekDates = null;
        salesPlanDirectlyAccuracyDTO.setStartTime(DateUtil.beginOfDay(DateUtil.beginOfWeek(startTime)));
        //按周
        if (null != endTime) {
            //结束时间设置为周日
            salesPlanDirectlyAccuracyDTO.setEndTime(DateUtil.beginOfDay(DateUtil.endOfWeek(endTime)));
            weekDates = MyDateUtil.getWeekDates(startTime, endTime);
        }
        if (null == endTime) {
            //默认14周
            weekDates = MyDateUtil.getWeekDates(startTime, DataDisplayCycleEnum.WEEKLY_FOURTEEN.getValue());
            salesPlanDirectlyAccuracyDTO.setEndTime(MyDateUtil.getLastDate(null, weekDates));
        }
        salesPlanDirectlyAccuracyDTO.setWeekDateList(weekDates);
        salesPlanDirectlyAccuracyDTO.setCreator(UserContext.getUserId());
        return salesPlanDirectlyAccuracyFacade.listAllByCycles(salesPlanDirectlyAccuracyDTO);
    }

    @Override
    public Response export(ExcelExportDTO excelExportDTO, Integer type) {
        if (type == null) {
            throw new RomeException(ResponseMsg.FAIL_2005.getCode(), ResponseMsg.FAIL_2005.getMsg());
        }
        SalesPlanDirectlyAccuracyDTO salesPlanDirectlyAccuracyDTO = new SalesPlanDirectlyAccuracyDTO();
        salesPlanDirectlyAccuracyDTO.setAreaCodeList(excelExportDTO.getAreaCodeList());
        salesPlanDirectlyAccuracyDTO.setLayeredCodeList(excelExportDTO.getLayeredCodeList());
        salesPlanDirectlyAccuracyDTO.setUnitType(excelExportDTO.getUnitType());
        salesPlanDirectlyAccuracyDTO.setCategoryCodeList(excelExportDTO.getCategoryCodeList());
        salesPlanDirectlyAccuracyDTO.setStartTime(excelExportDTO.getStartTime());
        //根据起始时间获取14周的日期数据
        List<Map<String, Date>> weekDates = MyDateUtil.getWeekDates(excelExportDTO.getStartTime(), DataDisplayCycleEnum.WEEKLY_FOURTEEN.getValue());
        salesPlanDirectlyAccuracyDTO.setWeekDateList(weekDates);
        //设置结束日期，方便sql查询命中索引
        salesPlanDirectlyAccuracyDTO.setEndTime(MyDateUtil.getLastDate(null, weekDates));
        //导出当页数据
        if (ExcelExportTypeEnum.CURRENT_PAGE_EXPORT.getType().equals(type)) {
            salesPlanDirectlyAccuracyDTO.setPageNum(excelExportDTO.getPageNum());
            salesPlanDirectlyAccuracyDTO.setPageSize(excelExportDTO.getPageSize());
        }
        //导出勾选
        if (ExcelExportTypeEnum.CHECKED_BOX_EXPORT.getType().equals(type)) {
            salesPlanDirectlyAccuracyDTO.setPageNum(excelExportDTO.getPageNum());
            salesPlanDirectlyAccuracyDTO.setPageSize(excelExportDTO.getPageSize());
            salesPlanDirectlyAccuracyDTO.setRowIndex(excelExportDTO.getRowIndex());
        }
        //导出全部数据
        //获取三个查询条件的实际集合
        List<SalesPlanSelectDTO> categorySalesPlanSelectDTOs = itemCoreFacade.getSubcategoryCode(excelExportDTO.getCategoryCodeList());
        BaseSearchUtil.setSearchFields(salesPlanDirectlyAccuracyDTO, null, categorySalesPlanSelectDTOs, null);
        salesPlanDirectlyAccuracyDTO.setCreator(UserContext.getUserId());
        return salesPlanDirectlyAccuracyFacade.export(salesPlanDirectlyAccuracyDTO, type);
    }
}
